package com.simple.rpc.config;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 系统配置文件
 *
 * @author mhh
 * @since 2020/10/20
 */
public class RpcSystemProperties {

    /**
     * 服务根路径
     */
    public static final String SERVICE_ROOT_PATH = "rpcServiceRootPath";

    /**
     * 默认的系统配置文件
     */
    private static final String PROPERTIES_PATH = "/singleRpc.properties";

    /**
     * 是否已经加载过
     */
    private static final AtomicBoolean LOADED = new AtomicBoolean(false);

    /**
     * 系统配置
     */
    private static final Properties SYS_PROPERTIES = new Properties();

    private RpcSystemProperties() {
    }

    /**
     * 加载配置文件
     */
    public static void load() {
        //加载系统配置文件
        try {
            SYS_PROPERTIES.load(RpcSystemProperties.class.getResourceAsStream(PROPERTIES_PATH));
            LOADED.set(true);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 为了保护系统配置
     *
     * @param key 配置键
     * @return 配置
     */
    public static String getProperty(String key) {
        if (LOADED.get()) {
            load();
        }
        return SYS_PROPERTIES.getProperty(key);
    }
}
